/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * author Svetlana Shikhutskaya
 */

var orders;
var totalNumber;
var pageNumber;
var count;
var oldcount;
var find;

function init(){
    sort=document.getElementById("sort-by").value;
    pageNumber=document.getElementById("page-number").value;
    loadOrders(sort);
}
function loadOrders(sortBy,right){
    //    alert('LoadOrders');
    $.post('LoadOrders',{
        pageNumber:pageNumber,
        sortBy:sortBy,
        find:find        
    },function(result){
        orders=$.parseJSON(result);
        if(count>0){
            oldcount=count;
        }
        count=orders.length;
        //        console.log(orders);
        if(count>0){
            DeleteRows();
            DisplayOrders();
        }
        else{
            if(right==1){
                pageNumber--;
            }
        }
        document.getElementById("num").innerHTML=pageNumber;
    })
    countPages();
}

function DisplayOrders(){
    for (var i=0;i<orders.length;i++){
        $('.table-orders').append('<tr id=row-'+i+'></tr>');
        $('.table-orders > tbody > tr:last').append('<td id="str"><span id="order-id-'+i+'">'+orders[i]["id"]+'</span></td>');
        $('.table-orders > tbody > tr:last').append('<td id="str"><span id="date-'+i+'">'+orders[i]["date"]+'</span></td>');
        $('.table-orders > tbody > tr:last').append('<td id="str"><span id="due-date-'+i+'">'+orders[i]["due-date"]+'</span></td>');
        $('.table-orders > tbody > tr:last').append('<td id="str"><span id="sum-'+i+'">'+orders[i]["sum"]+'</span></td>');
        $('.table-orders > tbody > tr:last').append('<td id="str"><span id="status-'+i+'">'+orders[i]["status"]+'</span></td>');
        $('.table-orders > tbody > tr:last').append('<td id="str"><span id="address-'+i+'">'+orders[i]["address"]+'</span></td>');
    }
}

$(function(){
    $(document).on('submit','.left-page',function(e){
        e.preventDefault();
        if(pageNumber!=1 && pageNumber !=0){
            pageNumber--;
            loadOrders(document.getElementById("sorts").value);
        }
    })
})

$(function(){
    $(document).on('submit','.right-page',function(e){
        e.preventDefault();
        pageNumber++;
        if(pageNumber<=totalNumber){
            loadOrders(document.getElementById("sorts").value,1);
        } else{
            pageNumber--;
        }
    })
})

function DeleteRows(){
    for (var i=0;i<oldcount;i++){
        $('#row-'+i).remove();
    }
}

$(function(){
    $(document).on('change','.sort-order',function(e){
        e.preventDefault();
        //        alert(document.getElementById("sorts").value);
        loadOrders(document.getElementById("sorts").value);
        document.getElementById('page-number').innerHTML=pageNumber;
    })
})
$(function(){
    $(document).on('submit','.find-order',function(e){
        e.preventDefault();
        find=document.getElementById("find-id").value;
        loadOrders();
    })
})

function countPages(){
    var number='1';
    $.post('CountRecords', {
        record: "order-history"
    }, function(result){
        if(result!="0"){
            number=result;
        }
        totalNumber=number;
        document.getElementById("total-num").innerHTML=number;
    })
}
function validateJumpPage(){
    var regex=new RegExp("^[0-9]+$");
    var str=document.getElementById("jump-page").value;
    document.getElementById("jump-warning").innerHTML="";
    if(str.length>0){
        if(regex.test(str)){
            str=parseInt(str);
            if(str!=0 && str<=totalNumber){
                document.getElementById("jump-warning").innerHTML="";
                document.getElementById("jump-btn").disabled=false;
            }else{
                document.getElementById("jump-warning").innerHTML="Such page doesn't exist";
                document.getElementById("jump-btn").disabled=true;
            }
        } else {
            document.getElementById("jump-warning").innerHTML="You can enter only numbers";
            document.getElementById("jump-btn").disabled=true;
        }
    } else {
        document.getElementById("jump-warning").innerHTML="";
    }
}

$(function(){
    $(document).on('submit','.js-jump-page',function(e){
        e.preventDefault();
        pageNumber=document.getElementById("jump-page").value;
        loadOrders(document.getElementById("sorts").value);
    })
})
